﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MemoryCard : MonoBehaviour
{
    //[SerializeField]将变量在Inspector中可见
    [SerializeField] private GameObject cardBack;
    [SerializeField] private SceneController controller;

    private int _id;

    public int id
    {
        get { return _id; }
    }

    // Use this for initialization
    void Start ()
	{
	    
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public void OnMouseDown()
    {
        //只是在对象当前激活/可见的情况下才运行反激活(deactivate)代码
        //检查控制器的canReveal属性确保同时只有两张卡片是翻开的
        if (cardBack.activeSelf && controller.canReveal)
        {
            //设置对象未不可见/不激活
            cardBack.SetActive(false);
            //当卡片显示时通知controller
            controller.CardRevealed(this);
        }
    }

    public void SetCard(int id, Sprite image)
    {
        _id = id;
        GetComponent<SpriteRenderer>().sprite = image;
    }

    //SceneController可以通过此方法再次隐藏卡片
    public void Unreveal()
    {
        cardBack.SetActive(true);
    }
}
